/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on Dec 20, 2008, 4:45:04 PM
 */
package GUI;

import Dictionary.Dictionary;
import GUI.other.*;
import GUI.other.confirmPanel;
import GUI.other.formException;
import VoiceIdent.SPitch;
import VoiceIdent.VoiceGUI;
import functions.Dogy;
import functions.LogError;
import java.awt.Color;
import java.awt.Component;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.sql.SQLException;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JDialog;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import javax.swing.KeyStroke;
import javax.swing.SwingWorker;
import javax.swing.UIManager;

/**
 *
 * @author GiK
 */
public class MainFrame extends javax.swing.JFrame {

    private Dogy manager;

    /** Creates new form MainFrame */
    public MainFrame(Dogy nManager) {
        
        initComponents();
        versionMenuItem.setText("Version v"+Dogy.VERSION+" - "+Dogy.V_PREFIX);
        if(Dogy.NEW_VERSION_READY)
        {
            helpMenu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/other/update_star.png")));
            JMenuItem item = new JMenuItem("New version ready, restart", new javax.swing.ImageIcon(getClass().getResource("/GUI/other/update_star.png")));
            item.setFont(new java.awt.Font("Tahoma", 1, 9));
            item.setToolTipText("Press to restart");
            item.addActionListener(new java.awt.event.ActionListener()
            {
            public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    try {
                        Runtime.getRuntime().exec("java -jar Updater.jar");
                    } catch (IOException ex) {
                        Exception(ex);
                    }
                  System.exit(0);
                }
            });
            helpMenu.add(item);
        }
        //System.out.println("ready " +Dogy.NEW_VERSION_READY);
        this.setTitle("DoGy ");
        manager = nManager;
        Search search = new Search(this);
        Sklad sklad = new Sklad(this);
        Otchet otchet = new Otchet(this);
        Catalog catalog = new Catalog(this);
        Doctors users;
        mainTabs.add(Dictionary.SEARCH_HEAL, search);
        mainTabs.add(Dictionary.REPORTS, otchet);
        mainTabs.add(Dictionary.WAREHOUSE, sklad);
        mainTabs.add(Dictionary.CATALOG, catalog);
        //jTabbedPane1.add("Профиль", null);
        if (manager != null) {
            if (manager.getDoctor().getStatus() == 1) {
                users = new Doctors(this);
                mainTabs.add(Dictionary.USERS, users);
            }
        }
        Dogy.reSize(this);

        KeyStroke keyStr = KeyStroke.getKeyStroke(KeyEvent.VK_F1,0);
        InputMap map = mainTabs.getInputMap(javax.swing.JTable.WHEN_IN_FOCUSED_WINDOW);
        map.put(keyStr, "f1");
        ActionMap actionMap = mainTabs.getActionMap();
        actionMap.put("f1", new AbstractAction() {
                     public void actionPerformed(ActionEvent actionEvent)
                     {
                         mainTabs.requestFocus();
                        mainTabs.setSelectedIndex(0);
                        
                     }
             });
        
        keyStr = KeyStroke.getKeyStroke(KeyEvent.VK_F2,0);
        map = mainTabs.getInputMap(javax.swing.JTable.WHEN_IN_FOCUSED_WINDOW);
        map.put(keyStr, "f2");
        actionMap = mainTabs.getActionMap();
        actionMap.put("f2", new AbstractAction() {
                     public void actionPerformed(ActionEvent actionEvent)
                     {
                         mainTabs.requestFocus();
                        mainTabs.setSelectedIndex(1);
                     }
             });
       keyStr = KeyStroke.getKeyStroke(KeyEvent.VK_F3,0);
        map = mainTabs.getInputMap(javax.swing.JTable.WHEN_IN_FOCUSED_WINDOW);
        map.put(keyStr, "f3");
        actionMap = mainTabs.getActionMap();
        actionMap.put("f3", new AbstractAction() {
                     public void actionPerformed(ActionEvent actionEvent)
                     {
                         mainTabs.requestFocus();
                         mainTabs.setSelectedIndex(2);
                     }
             });
        keyStr = KeyStroke.getKeyStroke(KeyEvent.VK_F4,0);
        map = mainTabs.getInputMap(javax.swing.JTable.WHEN_IN_FOCUSED_WINDOW);
        map.put(keyStr, "f4");
        actionMap = mainTabs.getActionMap();
        actionMap.put("f4", new AbstractAction() {
                     public void actionPerformed(ActionEvent actionEvent)
                     {
                         mainTabs.requestFocus();
                        mainTabs.setSelectedIndex(3);
                     }
             });
        keyStr = KeyStroke.getKeyStroke(KeyEvent.VK_F5,0);
        map = mainTabs.getInputMap(javax.swing.JTable.WHEN_IN_FOCUSED_WINDOW);
        map.put(keyStr, "f5");
        actionMap = mainTabs.getActionMap();
        actionMap.put("f5", new AbstractAction() {
                     public void actionPerformed(ActionEvent actionEvent)
                     {
                         mainTabs.requestFocus();
                        mainTabs.setSelectedIndex(4);
                     }
             });
    }

    // Exception handling
    public void SQLException(SQLException e) {
        LogError.reportSilentError(e);
        JOptionPane.showMessageDialog(null, "Ошибка базы данных: " + e.getMessage());
    }

    public void FormException(final Component comp, final String message) {
        SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                JDialog dial = new JDialog();
                dial.setFocusable(false);
                int y = comp.getLocationOnScreen().y + comp.getHeight()- dial.getHeight()-10;
                dial.add(new formException(message));
                dial.setUndecorated(true);
                dial.pack();
                dial.setLocation(
                        comp.getLocationOnScreen().x + comp.getWidth() / 2 - dial.getWidth() / 2,
                        y-dial.getHeight());
                dial.setVisible(true);
                
                Thread.sleep(3000);
                dial.dispose();
                return null;
            }
        };
        worker.execute();
    }

    public static boolean confirmForm(final Component comp, final String text) {


        JDialog dial = new JDialog();
        confirmPanel conf = new confirmPanel(text, comp);

        
        conf.setVisible(true);
        dial.add(conf);
        dial.setModal(true);
        dial.setUndecorated(true);
        dial.pack();
        int y = comp.getLocationOnScreen().y + comp.getHeight() - dial.getHeight()-10;
        dial.setLocation(
                comp.getLocationOnScreen().x + comp.getWidth() / 2 - dial.getWidth() / 2,
                y);
        dial.setVisible(true);


        return conf.getResult();
    }
    public static boolean okForm(final Component comp, final String text) {


        JDialog dial = new JDialog();
        okPanel ok = new okPanel(text, comp);

        int y = comp.getLocationOnScreen().y + comp.getHeight() - dial.getHeight()-10;
        ok.setVisible(true);
        dial.add(ok);
        dial.setModal(true);
        dial.setUndecorated(true);
        dial.pack();
        dial.setLocation(
                comp.getLocationOnScreen().x + comp.getWidth() / 2 - dial.getWidth() / 2,
                y);
        dial.setVisible(true);

        return ok.getResult();
    }
    
    public Dogy getManager() {
        return manager;
    }

    public void Exception(Exception ex) 
    {
        LogError.reportSilentError(ex);
        JOptionPane.showMessageDialog(null, "Ошибка : " + ex.getMessage());
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainTabs = new javax.swing.JTabbedPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        leaveFeedBackMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        versionMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });

        mainTabs.setFont(new java.awt.Font("Tahoma", 1, 16));
        mainTabs.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                mainTabsStateChanged(evt);
            }
        });

        jMenuBar1.setMaximumSize(new java.awt.Dimension(50, 32769));
        jMenuBar1.setMinimumSize(new java.awt.Dimension(50, 21));
        jMenuBar1.setPreferredSize(new java.awt.Dimension(50, 21));

        jMenu2.setText("File");

        jMenuItem1.setText("Настройки");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("sPitch");
        jMenu3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu3ActionPerformed(evt);
            }
        });

        jMenuItem2.setText("Run");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        jMenuItem3.setText("Settings");
        jMenu3.add(jMenuItem3);

        jMenuBar1.add(jMenu3);

        helpMenu.setText("Помощь");

        leaveFeedBackMenuItem.setText("Оставить отзыв");
        leaveFeedBackMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                leaveFeedBackMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(leaveFeedBackMenuItem);

        aboutMenuItem.setText("About");
        helpMenu.add(aboutMenuItem);
        helpMenu.add(jSeparator1);

        versionMenuItem.setFont(new java.awt.Font("Tahoma", 1, 9));
        versionMenuItem.setText("Version");
        versionMenuItem.setMargin(new java.awt.Insets(2, 0, 2, 2));
        versionMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                versionMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(versionMenuItem);

        jMenuBar1.add(helpMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainTabs, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 859, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainTabs, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mainTabsStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_mainTabsStateChanged
        this.setTitle("Dogy - " + mainTabs.getTitleAt(mainTabs.getSelectedIndex()));
        
}//GEN-LAST:event_mainTabsStateChanged

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        Settings set = new Settings(this);
        set.pack();
        set.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenu3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenu3ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        final int cX = this.getX(),
                  cY = this.getY(),
                  cW = this.getWidth();
        final MainFrame fr = this;
        //SwingWorker sw = new SwingWorker()
        //{
        //    @Override
        //    protected Object doInBackground() throws Exception {
                SPitch.start(fr, cX+cW,cY);
       //         return fr;
       //     }
       // };
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
        
    }//GEN-LAST:event_formFocusGained

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
        System.out.println("Focus gained");
        if(SPitch.isStarted())
            ;//SPitch.toFront();
        //this.toFront();
    }//GEN-LAST:event_formWindowGainedFocus

    private void versionMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_versionMenuItemActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_versionMenuItemActionPerformed

    private void leaveFeedBackMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_leaveFeedBackMenuItemActionPerformed
        LeaveFeedback lf = new LeaveFeedback(this);
        Dogy.placeToCenter(lf);
        lf.setVisible(true);
}//GEN-LAST:event_leaveFeedBackMenuItemActionPerformed
    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JMenuItem leaveFeedBackMenuItem;
    private javax.swing.JTabbedPane mainTabs;
    private javax.swing.JMenuItem versionMenuItem;
    // End of variables declaration//GEN-END:variables
}
